var gdata = {};
function del(id) {
    $.ajax({
        url: '/shop/' + id,
        type: 'DELETE',
        success: function (result) {
            window.location.reload();
        }
    });
}

function add() {
    $.ajax({
        url: '/shop',
        type: 'POST',
        data: $("form").serialize(),
        success: function (data) {
            window.location.reload();
        }
    });
}

function show(id) {
    $("#id").val(id);
    var record = gdata[id];
    $("#shop_name").val(record.shop_name);
    $("#shop_address").val(record.shop_address);
    $("#shop_telephone").val(record.shop_telephone);
}

function update() {
   
    $.ajax({
        url: '/shop/' + $("#id").val(),
        type: 'PUT',
        data: $("form").serialize(),
        success: function (data) {
            window.location.reload();
        }
    });
}


$(function () {
    $.get("/shop", {}, function (data) {
        var out = [];
        for (var i = 0; i < data.data.length; i++) {
            gdata["" + data.data[i].id] = data.data[i];
            out.push("<tr>")
            out.push("<td>" + data.data[i].id + "</td>");
            out.push("<td>" + data.data[i].shop_name + "</td>");
            out.push("<td>" + data.data[i].shop_address + "</td>");
            out.push("<td>");
            out.push("<a href=\"javascript:del('" + data.data[i].id + "')\">删除</a>");
            out.push("<a href=\"javascript:show('" + data.data[i].id + "')\">修改</a>");
            out.push("</td>");
            out.push("</tr>");
        }
        $("#tbody").html(out.join(""))
    }, "json")
})